package com.new2i.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import org.apache.log4j.Logger;

/**
 * 发送短信工具类
 * 
 * @since 2015/09/06
 * @author 2i
 *
 */
public class SmsSendUtil {

	private static Logger logger = Logger.getLogger(SmsSendUtil.class);

	/**
	 * 发送短信
	 * 
	 * @since 2015／09/06
	 * @author 2i
	 * @param mobile
	 * @param content
	 */
	public static String sendSms(String mobile, String content) {

		// 账号密码SDK-WSS-010-066896eD15-c2
		String action = "http://sdk2.zucp.net:8060/z_mdsmssend.aspx";// 短信接口地址
		String sn = "SDK-WSS-010-06689";// sn代码
		String pwd = "1FFB25119DEB05CA1F44C7BFD5258269";// 密码
 
		HttpURLConnection http = null;
		BufferedReader in = null;
		try { 
			URL url = new URL(action);
			http = (HttpURLConnection) url.openConnection();
			http.setRequestMethod("POST");
			http.setConnectTimeout(0);
			http.setInstanceFollowRedirects(true);
			http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=gbk");
			http.setDefaultUseCaches(false);
			http.setDoOutput(true);
			String queryString = "sn=" + sn + "&pwd=" + pwd + "&mobile=" + mobile + "&content="
					+ URLEncoder.encode(content + "【视琦科技】", "gbk") // 已加签名,必须转换gbk
					+ "&ext=&rrid=&stime=";

			PrintWriter out = new PrintWriter(http.getOutputStream());
			out.print(queryString);// 传入参数
			out.close();

			http.connect();// 发送请求
			in = new BufferedReader(new InputStreamReader(http.getInputStream()));
			String result = in.readLine();// 获取返回值
			logger.debug("send sms result==>" + result);
			http.disconnect();
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

		}
		return null;
	}
}
